package com.example.makerspace.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.makerspace.domain.BaseDO;
import com.example.makerspace.domain.DemandDO;
import com.example.makerspace.domain.UserDO;
import com.example.makerspace.dto.Pagination;
import com.example.makerspace.mapper.DemandMapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;

@Component
public class DemandDAO extends BaseDAO<DemandDO>{
    @Resource
    private DemandMapper demandMapper;

    @PostConstruct
    public void init(){
        this.mapper = demandMapper;
    }
    public final static String DEMAND_NAME = "demand_name";
    public final static String DEMAND_DESC = "demand_desc";
    public final static String KEYWORD = "keyword";
    public final static String CATEGORY = "category";

    public DemandDO selectByDemandName(String demandName){
        return demandMapper.selectOne(new QueryWrapper<DemandDO>().eq(DEMAND_NAME, demandName));
    }

    public List<DemandDO> selectByIds(List<Integer> ids) {
        return demandMapper.selectBatchIds(ids);
    }

    public IPage<DemandDO> selectByPage(Pagination pagination) {
        QueryWrapper<DemandDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_show",1).orderByDesc("praise");
        return  mapper.selectPage(new Page<DemandDO>(pagination.getPageNum(), pagination.getSize()),queryWrapper);
    }

    public IPage<DemandDO> selectByPageAndLike(Pagination pagination, String demandName,String category) {
        QueryWrapper<DemandDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category",category).like("demand_name",demandName).eq("is_show",1).orderByDesc("create_time");
        return  mapper.selectPage(new Page<DemandDO>(pagination.getPageNum(), pagination.getSize()),queryWrapper);
    }

    public IPage<DemandDO> selectByPageAndCatgory(Pagination pagination, String category) {
        QueryWrapper<DemandDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category",category).eq("is_show",1).orderByDesc("create_time");
        return  mapper.selectPage(new Page<DemandDO>(pagination.getPageNum(), pagination.getSize()),queryWrapper);
    }

    public IPage<DemandDO> selectByPageAndLike2(Pagination pagination, String demandName) {
        QueryWrapper<DemandDO> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("is_show",1).like("demand_name",demandName).orderByDesc("praise");
        return  mapper.selectPage(new Page<DemandDO>(pagination.getPageNum(), pagination.getSize()),queryWrapper);
    }

    public void deleteByDemandName(String demandName) {
        mapper.delete(new QueryWrapper<DemandDO>().eq("demand_name",demandName));
    }

    public IPage<DemandDO> selectByPageAndUserName(Pagination pagination, String username) {
        QueryWrapper<DemandDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username).orderByDesc("praise");
        return  mapper.selectPage(new Page<DemandDO>(pagination.getPageNum(), pagination.getSize()),queryWrapper);
    }

    public IPage<DemandDO> selectByPageAndShow(Pagination pagination, String username, String isShow) {
        QueryWrapper<DemandDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username).eq("is_show",isShow).orderByDesc("praise");
        return  mapper.selectPage(new Page<DemandDO>(pagination.getPageNum(), pagination.getSize()),queryWrapper);
    }

    public IPage<DemandDO> selectByPageAndDemandName(Pagination pagination, String username, String demandName) {
        QueryWrapper<DemandDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username).like("demand_name",demandName).orderByDesc("praise");
        return  mapper.selectPage(new Page<DemandDO>(pagination.getPageNum(), pagination.getSize()),queryWrapper);
    }

    public IPage<DemandDO> selectByPageAndDemandNameAndShow(Pagination pagination, String username, String demandName, String isShow) {
        QueryWrapper<DemandDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username).eq("is_show",isShow).like("demand_name",demandName).orderByDesc("praise");
        return  mapper.selectPage(new Page<DemandDO>(pagination.getPageNum(), pagination.getSize()),queryWrapper);
    }

    public List<DemandDO> selectByUserName(String username) {
        QueryWrapper<DemandDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username).orderByDesc("modify_time");
        return  mapper.selectList(queryWrapper);
    }

    public IPage<DemandDO> selectByAllPage(Pagination pagination) {
        QueryWrapper<DemandDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("create_time");
        return  mapper.selectPage(new Page<DemandDO>(pagination.getPageNum(), pagination.getSize()),queryWrapper);
    }

    public DemandDO selectByIdAndisshow(Integer id) {
        QueryWrapper<DemandDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id).eq("is_show",1);
        return  mapper.selectOne(queryWrapper);
    }

    public DemandDO selectByContent(String productOverview) {
        QueryWrapper<DemandDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("product_overview",productOverview);
        return  mapper.selectOne(queryWrapper);
    }

    public IPage<DemandDO> selectByDemandNamePage(Pagination pagination, String demandName) {
        QueryWrapper<DemandDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("demand_name",demandName);
        queryWrapper.orderByDesc("create_time");
        return  mapper.selectPage(new Page<DemandDO>(pagination.getPageNum(), pagination.getSize()),queryWrapper);
    }

    public IPage<DemandDO> selectByCategoryPage(Pagination pagination, String category) {
        QueryWrapper<DemandDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category",category);
        queryWrapper.orderByDesc("create_time");
        return  mapper.selectPage(new Page<DemandDO>(pagination.getPageNum(), pagination.getSize()),queryWrapper);
    }

    public IPage<DemandDO> selectByisShowPage(Pagination pagination, String isShow) {
        QueryWrapper<DemandDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_show",isShow);
        queryWrapper.orderByDesc("create_time");
        return  mapper.selectPage(new Page<DemandDO>(pagination.getPageNum(), pagination.getSize()),queryWrapper);
    }

    public List<DemandDO> selectOrderTime() {
        QueryWrapper<DemandDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("create_time");
        queryWrapper.eq("is_show",1);
        return  mapper.selectList(queryWrapper);
    }


    public IPage<DemandDO> selectByPageAndCatgoryShowNumberDesc(Pagination pagination, String category) {
        QueryWrapper<DemandDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category",category).eq("is_show",1).orderByDesc("show_number");
        queryWrapper.orderByDesc("create_time");
        return  mapper.selectPage(new Page<DemandDO>(pagination.getPageNum(), pagination.getSize()),queryWrapper);
    }

    public IPage<DemandDO> selectByPageAndCatgoryShowNumberASC(Pagination pagination, String category) {
        QueryWrapper<DemandDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category",category).eq("is_show",1).orderByAsc("show_number");
        queryWrapper.orderByDesc("create_time");
        return  mapper.selectPage(new Page<DemandDO>(pagination.getPageNum(), pagination.getSize()),queryWrapper);
    }

    public IPage<DemandDO> selectByPageAndCatgoryPraiseDesc(Pagination pagination, String category) {
        QueryWrapper<DemandDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category",category).eq("is_show",1).orderByDesc("praise");
        queryWrapper.orderByDesc("create_time");
        return  mapper.selectPage(new Page<DemandDO>(pagination.getPageNum(), pagination.getSize()),queryWrapper);
    }
    public IPage<DemandDO> selectByPageAndCatgoryPraiseASC(Pagination pagination, String category) {
        QueryWrapper<DemandDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category",category).eq("is_show",1).orderByAsc("praise");
        queryWrapper.orderByDesc("create_time");
        return  mapper.selectPage(new Page<DemandDO>(pagination.getPageNum(), pagination.getSize()),queryWrapper);
    }

    public IPage<DemandDO> selectByPageAndCatgoryCollectionDesc(Pagination pagination, String category) {
        QueryWrapper<DemandDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category",category).eq("is_show",1).orderByDesc("collection");
        queryWrapper.orderByDesc("create_time");
        return  mapper.selectPage(new Page<DemandDO>(pagination.getPageNum(), pagination.getSize()),queryWrapper);
    }

    public IPage<DemandDO> selectByPageAndCatgoryCollectionASC(Pagination pagination, String category) {
        QueryWrapper<DemandDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category",category).eq("is_show",1).orderByAsc("collection");
        queryWrapper.orderByDesc("create_time");
        return  mapper.selectPage(new Page<DemandDO>(pagination.getPageNum(), pagination.getSize()),queryWrapper);
    }

    public IPage<DemandDO> selectByPageAndLikeShowNubmerDesc(Pagination pagination, String demandName, String category) {
        QueryWrapper<DemandDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category",category).like("demand_name",demandName).eq("is_show",1).orderByDesc("show_number");
        return  mapper.selectPage(new Page<DemandDO>(pagination.getPageNum(), pagination.getSize()),queryWrapper);
    }

    public IPage<DemandDO> selectByPageAndLikeShowNubmerASC(Pagination pagination, String demandName, String category) {
        QueryWrapper<DemandDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category",category).like("demand_name",demandName).eq("is_show",1).orderByAsc("show_number");
        return  mapper.selectPage(new Page<DemandDO>(pagination.getPageNum(), pagination.getSize()),queryWrapper);
    }

    public IPage<DemandDO> selectByPageAndLikePraiseDesc(Pagination pagination, String demandName, String category) {
        QueryWrapper<DemandDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category",category).like("demand_name",demandName).eq("is_show",1).orderByDesc("praise");
        return  mapper.selectPage(new Page<DemandDO>(pagination.getPageNum(), pagination.getSize()),queryWrapper);
    }

    public IPage<DemandDO> selectByPageAndLikePraiseASC(Pagination pagination, String demandName, String category) {
        QueryWrapper<DemandDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category",category).like("demand_name",demandName).eq("is_show",1).orderByAsc("praise");
        return  mapper.selectPage(new Page<DemandDO>(pagination.getPageNum(), pagination.getSize()),queryWrapper);
    }

    public IPage<DemandDO> selectByPageAndLikeCollectionDesc(Pagination pagination, String demandName, String category) {
        QueryWrapper<DemandDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category",category).like("demand_name",demandName).eq("is_show",1).orderByDesc("collection");
        return  mapper.selectPage(new Page<DemandDO>(pagination.getPageNum(), pagination.getSize()),queryWrapper);
    }

    public IPage<DemandDO> selectByPageAndLikeCollectionASC(Pagination pagination, String demandName, String category) {
        QueryWrapper<DemandDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category",category).like("demand_name",demandName).eq("is_show",1).orderByAsc("collection");
        return  mapper.selectPage(new Page<DemandDO>(pagination.getPageNum(), pagination.getSize()),queryWrapper);
    }

    public IPage<DemandDO> selectByPageAndShowNumberDesc(Pagination pagination) {
        QueryWrapper<DemandDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_show",1).orderByDesc("show_number");
        return  mapper.selectPage(new Page<DemandDO>(pagination.getPageNum(), pagination.getSize()),queryWrapper);
    }

    public IPage<DemandDO> selectByPageAndShowNumberASC(Pagination pagination) {
        QueryWrapper<DemandDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_show",1).orderByAsc("show_number");
        return  mapper.selectPage(new Page<DemandDO>(pagination.getPageNum(), pagination.getSize()),queryWrapper);
    }

    public IPage<DemandDO> selectByPageAndPraiseDesc(Pagination pagination) {
        QueryWrapper<DemandDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_show",1).orderByDesc("praise");
        return  mapper.selectPage(new Page<DemandDO>(pagination.getPageNum(), pagination.getSize()),queryWrapper);
    }

    public IPage<DemandDO> selectByPageAndPraiseASC(Pagination pagination) {
        QueryWrapper<DemandDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_show",1).orderByAsc("praise");
        return  mapper.selectPage(new Page<DemandDO>(pagination.getPageNum(), pagination.getSize()),queryWrapper);
    }

    public IPage<DemandDO> selectByPageAndCollectionDesc(Pagination pagination) {
        QueryWrapper<DemandDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_show",1).orderByDesc("collection");
        return  mapper.selectPage(new Page<DemandDO>(pagination.getPageNum(), pagination.getSize()),queryWrapper);
    }

    public IPage<DemandDO> selectByPageAndCollectionASC(Pagination pagination) {
        QueryWrapper<DemandDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_show",1).orderByAsc("collection");
        return  mapper.selectPage(new Page<DemandDO>(pagination.getPageNum(), pagination.getSize()),queryWrapper);
    }

    public void deleteByUsername(String username) {
        mapper.delete(new QueryWrapper<DemandDO>().eq("username",username));
    }

    public IPage<DemandDO> selectByPageAndCommend(Pagination pagination, List<Integer> collect) {
        QueryWrapper<DemandDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_show",1);
        queryWrapper.in("id",collect);
        return  mapper.selectPage(new Page<DemandDO>(pagination.getPageNum(), pagination.getSize()),queryWrapper);
    }

    public  IPage<DemandDO>  getDemandDOSByCondition(Page<DemandDO> page,String demandName, String category,
                                                      String showNumber, String praise,
                                                     String collection){
        return page.setRecords(demandMapper.getDemandDOSByCondition(page,demandName,category,showNumber,praise,collection));
    }
    public  IPage<DemandDO> getDemandDOS(Page<DemandDO> page){

        return page.setRecords(demandMapper.getDemandDOS(page));
    }

    public List<DemandDO> selectByProvince(String province) {
        QueryWrapper<DemandDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("company_address",province);
        return mapper.selectList(queryWrapper);
    }

    public List<DemandDO> selectAllByIsShow() {
        QueryWrapper<DemandDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_show",1);
        return  mapper.selectList(queryWrapper);
    }

    public  IPage<DemandDO>  selectByPageAndLikeName(Page<DemandDO> page,String name) {
        LambdaQueryWrapper<DemandDO> qw = new LambdaQueryWrapper<DemandDO>()
                .like(StringUtils.isNotEmpty(name),DemandDO::getDemandName,name).or().like(StringUtils.isNotEmpty(name),DemandDO::getSerialNumber,name)
                .orderByDesc(DemandDO::getCreateTime);
        return  mapper.selectPage(page,qw);
    }

    public IPage<DemandDO> selectByPageAndLikeNameStatus(Page<DemandDO> page, String name, String status) {
        Integer isShow = null;
        if(!status.equals("")){
            isShow = Integer.parseInt(status);
        }
        LambdaQueryWrapper<DemandDO> qw = new LambdaQueryWrapper<DemandDO>();
        qw.eq(StringUtils.isNotEmpty(status), DemandDO::getIsShow, isShow).and(wrapper ->
                        wrapper.like(StringUtils.isNotEmpty(name),DemandDO::getDemandName,name).or()
                                .like(StringUtils.isNotEmpty(name),DemandDO::getSerialNumber,name).or().like(StringUtils.isNotEmpty(name),DemandDO::getCompanyName,name)).orderByDesc(DemandDO::getCreateTime);
        return  mapper.selectPage(page,qw);

    }

    public IPage<DemandDO> selectByPageAndTime(Page<DemandDO> page, String startTime, String endTime) {

        LambdaQueryWrapper<DemandDO> qw = new LambdaQueryWrapper<DemandDO>();
         qw .le(DemandDO::getCreateTime,endTime).ge(DemandDO::getCreateTime,startTime).orderByDesc(DemandDO::getCreateTime);
        return  mapper.selectPage(page,qw);
    }

    public IPage<DemandDO> selectByPageStatus(Page<DemandDO> page, String status) {
        Integer isShow = null;
        if(!status.equals("")){
            isShow = Integer.parseInt(status);
        }
        LambdaQueryWrapper<DemandDO> qw = new LambdaQueryWrapper<DemandDO>();
        qw.eq(StringUtils.isNotEmpty(status), DemandDO::getIsShow, isShow).orderByDesc(DemandDO::getCreateTime);
        return  mapper.selectPage(page,qw);
    }

    public IPage<DemandDO> selectByPageAndAll(Page<DemandDO> page, String name, String status, String startTime, String endTime) {
        Integer isShow = null;
        if(!status.equals("")){
            isShow = Integer.parseInt(status);
        }
        LambdaQueryWrapper<DemandDO> qw = new LambdaQueryWrapper<DemandDO>();
        qw.eq(StringUtils.isNotEmpty(status), DemandDO::getIsShow, isShow).and(wrapper ->
                wrapper.like(StringUtils.isNotEmpty(name),DemandDO::getDemandName,name).or()
                        .like(StringUtils.isNotEmpty(name),DemandDO::getSerialNumber,name).or().like(StringUtils.isNotEmpty(name),DemandDO::getCompanyName,name))
                .le(DemandDO::getCreateTime,endTime).ge(DemandDO::getCreateTime,startTime).orderByDesc(DemandDO::getCreateTime);
        return  mapper.selectPage(page,qw);
    }

    public IPage<DemandDO> selectByPageStatusAndTime(Page<DemandDO> page, String startTime, String endTime, String status) {
        Integer isShow = null;
        if(!status.equals("")){
            isShow = Integer.parseInt(status);
        }
        LambdaQueryWrapper<DemandDO> qw = new LambdaQueryWrapper<DemandDO>();
        qw.eq(StringUtils.isNotEmpty(status), DemandDO::getIsShow, isShow).and(wrapper ->
                wrapper.le(DemandDO::getCreateTime,endTime).ge(DemandDO::getCreateTime,startTime));
        qw.orderByDesc(BaseDO::getCreateTime);
        return  mapper.selectPage(page,qw);
    }
}
